package com.tdam_2012_g7;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.ListActivity;
import adapters.ItemTelefonoAdapter;//*
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.tdam_2012_g7.R;
import com.tdam_2012_g7.data.RegistrarAccion;

import entidades.Accion;
import entidades.Contacto;
import entidades.ContactoTelefono;

public class Activity_Seleccionar_Sms extends ListActivity implements
OnItemClickListener {
	
	ArrayList<Contacto> Contactos;
	ArrayList<ContactoTelefono> Telefonos;
	String strId;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Contactos = new ArrayList<Contacto>();
        Contactos = getContactos();
        Telefonos = new ArrayList<ContactoTelefono>();
        Bundle bundle=getIntent().getExtras();
        strId = bundle.getString("Id");
        ArrayList<ContactoTelefono> items = getItems(strId);
        setContentView(R.layout.activity_mandar_sms);
        
        setListAdapter(new ItemTelefonoAdapter(this, R.layout.item_sms,items));
        getListView().setOnItemClickListener(this);
        
    }
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1,
			int position, long arg3) {
		ContactoTelefono telefono = new ContactoTelefono();
		telefono = (ContactoTelefono) Telefonos.get(position);
		
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:+"+telefono.getValue()));
		
		SimpleDateFormat dateFormat;
		dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		Context contexto = getApplicationContext();
		Contacto cont = new Contacto();
		cont.setId(strId);
		Date date = new Date();
		Accion act = new Accion();
    	act.setDescripcion("SMS enviado");
    	act.setFecha(dateFormat.format(date));
    	act.setIdContacto(Integer.parseInt(strId));
    	act.setNombreAccion("SMS");
    	act.setTipoIdAccion(2);
    	RegistrarAccion regAcc = new RegistrarAccion();
    	regAcc.regAccion(cont, act, contexto);
    	
		startActivity(intent);
	}
	
	
	 public ArrayList<ContactoTelefono> getItems(String Id) {
						
			
			// Recorre la lista de Contactos y agrega los telefonos del seleccionado
			
			for (Contacto cont: Contactos)
			{
				  if (cont.getId().equals(Id))
				  {
					  Telefonos = cont.getTelefonos();
				  }
				  
			}


			return Telefonos;
		}
	 public ArrayList<Contacto> getContactos() {
		 ArrayList<Contacto> miLista = new ArrayList<Contacto>();
			ContentResolver cr = getContentResolver();
			Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
					null, null, null);

			Contacto contact = null;

			if (cur.getCount() > 0) {
				while (cur.moveToNext()) {
					String id = cur.getString(cur
							.getColumnIndex(ContactsContract.Contacts._ID));
					String name = cur
							.getString(cur
									.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

					contact = new Contacto();
					contact.setNombre(name);
					contact.setId(id);

					if (Integer
							.parseInt(cur.getString(cur
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
						loadContactTelephoneNumbers(contact);
						loadContactEmails(contact);
					}

					miLista.add(contact);
				}
			}
			
			
			cur.close();

			return miLista;
		}
	 private void loadContactTelephoneNumbers(Contacto contact) {
			Cursor pCur = getContentResolver().query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
					new String[] { contact.getId() }, null);
			while (pCur.moveToNext()) {
				String numero = pCur
				.getString(pCur
						.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				String tipoNumero = pCur
				.getString(pCur
						.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
				
				
				contact.agregarTelefono(numero, tipoNumero);
			}
			pCur.close();

		}
	 

		private void loadContactEmails(Contacto contact) {
			Cursor emailCur = getContentResolver().query(
					ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
					new String[] { contact.getId() }, null);
			while (emailCur.moveToNext()) {
				// This would allow you get several email addresses
				// if the email addresses were stored in an array
				String email = emailCur
						.getString(emailCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
				String emailType = emailCur
						.getString(emailCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
				contact.agregarEmail(email, emailType);
			}
			emailCur.close();

		}

}